#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "CheckerBoard.h"

using namespace std;

void CheckerBoard::moveAI(int type)
{
	if (type==0){
		if (turn==BLACK){
			moveBlack1();
		}else {
			moveWhite1();
		}
	}else if (type==1){
		if (turn==BLACK){
			moveBlack2();
		}else {
			moveWhite2();
		}
	}
	else {
		if (turn==BLACK){
			moveBlack3();
		}else {
			moveWhite2();
		}
	}
}

void CheckerBoard::moveBlack1()
{
	bool isMoved = false;
	int x = (size/2)-1, y = size-1, i = 0;
	int moveState = 0; 			//0=belom jalan, 1=UdahGakAman, 2 =  Matilah, 3 = King Move Biasa
	if (!isEmpty(LoEat)){
		checkJump(jumpAIx,jumpAIy);
		move(getX(LoJump,0),getY(LoJump,0),getX(LoJump,1),getY(LoJump,1));
	}else{
		if (!isEmpty(LoDanger)){
			x = getX(LoDanger,i);
			y = getY(LoDanger,i);
		}
		while (!isMoved){
			if (isExistBlackPawn(x,y)){
				if(!isEmpty(LoDanger)){
					x = getX(LoDanger,i);
					y = getY(LoDanger,i);				
					isMoved = backupBlack(x,y);		// Kasih Backup
					if (!isMoved && moveState==1){
						if (Board[x][y]->isKing){			// Kabur King
							isMoved = escapePawnKingBlack(x,y);
						}else{						// Kabur Kroco
							isMoved = escapePawnBlack(x,y);
						}
					}
				}else{ 
					if (Board[x][y]->isKing && moveState!=3){
						if (!isExistNormalPawn()){isMoved = movePawnKingBlack(moveState,x,y);}
					} else if (isExistNormalPawn() && Board[x][y]->isKing && moveState==3){
						isMoved = movePawnKingImmediate(x,y);
					}  else { 						
						isMoved = movePawnBlack(moveState,x,y);
					}
				}				
			}
			
			if (!isMoved){
				if (!isEmpty(LoDanger)){
					i++;
					if (i==sizeLOP(LoDanger)) { 
						i=0;
						x=(size/2)-1;
						y=-1;
					}
				}
				
				x--;
				if (x==-1){ x=(size/2)-1; y--; }
				if (y == -1){
					if (moveState==0){moveState=1;}
					else if (moveState==1){
						moveState=2;
						clearList(&LoDanger);
					}else if (moveState==2){ moveState=3;}
					x=(size/2)-1;
					y=size-1;
				}
			}
		}
	}
}

void CheckerBoard::moveWhite1()
{
	bool isMoved = false;
	int i = 0, x = 0, y = 0;
	int moveState = 0; 			//0=belom jalan, 1=UdahGakAman, 2 =  Matilah
	if (!isEmpty(LoEat)){
		checkJump(jumpAIx, jumpAIy);
		move(getX(LoJump,0),getY(LoJump,0),getX(LoJump,1),getY(LoJump,1));
	}else{
		if (!isEmpty(LoDanger)){
			x = getX(LoDanger,i);
			y = getY(LoDanger,i);
		}		
		while (!isMoved){
			if (isExistWhitePawn(x,y)){
				if(!isEmpty(LoDanger)){ 
					x = getX(LoDanger,i);
					y = getY(LoDanger,i);					
					isMoved = backupWhite(x,y);		// Kasih Backup
					
					if (!isMoved && moveState==1){
						if (Board[x][y]->isKing){			// Kabur King
							isMoved = escapePawnKingWhite(x,y);
						}else{						// Kabur Kroco
							isMoved = escapePawnWhite(x,y);
						}
					}
				}else{
					if (Board[x][y]->isKing && moveState!=3){
						if (!isExistNormalPawn()){isMoved = movePawnKingWhite(moveState,x,y);}
					} else if (isExistNormalPawn() && Board[x][y]->isKing && moveState==3){
						isMoved = movePawnKingImmediate(x,y);
					} else{
						isMoved = movePawnWhite(moveState,x,y);
					}
				}					
			}
			
			if (!isMoved){
				if (!isEmpty(LoDanger)){
					i++;
					if (i==sizeLOP(LoDanger)) { 
						i=0;
						x=0;
						y=size;
					}
				}
				
				x++;
				if (x==size/2){ x=0; y++; }
				if (y==size){
					if (moveState==0){moveState=1;}
					else if (moveState==1){
						moveState=2;
						clearList(&LoDanger);
					}else if (moveState==2){ moveState=3;}
					x=0;
					y=0;
				}
			}
		}
	}
}

void CheckerBoard::moveBlack2()
{
	bool isMoved = false;
	int x = 0, y = 0, i = 0;
	int moveState = 0; 			//0=belom jalan, 1=UdahGakAman, 2 =  Matilah
	ListOfPoint LoKing = NULL;
	searchKing(&LoKing);
	printList(LoKing);
	if (!isEmpty(LoEat)){
		checkJump(jumpAIx,jumpAIy);
		move(getX(LoJump,0),getY(LoJump,0),getX(LoJump,1),getY(LoJump,1));
	}else{
		if (isEmpty(LoKing) || !isEmpty(LoDanger)){
			while (!isMoved){
				terpaksa:
				if (isExistBlackPawn(x,y)){
					if(!isEmpty(LoDanger)){
						x = getX(LoDanger,i);
						y = getY(LoDanger,i);	
						
						if (Board[x][y]->isKing){			// Kabur King
							isMoved = escapePawnKingBlack(x,y);
						}else{						// Kabur Kroco
							isMoved = escapePawnBlack(x,y);
						}

						if (!isMoved && moveState==1){	
							isMoved = backupBlack(x,y);		// Kasih Backup
						}
					}else{ 
						if (Board[x][y]->isKing){
							isMoved = movePawnKingBlack(moveState,x,y);
						}else{
							isMoved = movePawnBlack(moveState,x,y);
						}
					}				
				}
				
				if (!isMoved){
					if (!isEmpty(LoDanger)){
						if (moveState==0){
							i++;
							if (i==sizeLOP(LoDanger) && moveState==0) { 
								i=0;
								x=0;
								y=size;
							}
						}
					}
					
					x++;
					if (x==size/2){ x=0; y++; }
					if (y == size){
						if (moveState==0){moveState=1;}
						if (moveState==1){
							moveState=2;
							clearList(&LoDanger);
						}
						x=0;
						y=0;
					}
				}
			}
		} else {
			i = 1;
			while (!isMoved){
				x = getX(LoKing,sizeLOP(LoKing)-i);
				y = getY(LoKing,sizeLOP(LoKing)-i);
				
				isMoved = movePawnKingBlack(moveState,x,y); 
				
				if (i==sizeLOP(LoKing)){
					i=1;
					clearList(&LoKing);
					goto terpaksa;
				} else {
					i++;
				}
			}
		}
	}
}

void CheckerBoard::moveWhite2()
{
	bool isMoved = false;
	int x = (size/2)-1, y = size-1, i = 0;
	int moveState = 0; 			//0=belom jalan, 1=UdahGakAman, 2 =  Matilah
	ListOfPoint LoKing = NULL;
	searchKing(&LoKing);
	printList(LoKing);
	if (!isEmpty(LoEat)){
		checkJump(jumpAIx, jumpAIy);
		move(getX(LoJump,0),getY(LoJump,0),getX(LoJump,1),getY(LoJump,1));
	}else{
		if (isEmpty(LoKing) || !isEmpty(LoDanger)){
			while (!isMoved){
				terpaksa:
				if (isExistWhitePawn(x,y)){
					if(!isEmpty(LoDanger)){ 
						x = getX(LoDanger,i);
						y = getY(LoDanger,i);
						
						if (Board[x][y]->isKing){			// Kabur King
							isMoved = escapePawnKingWhite(x,y);
						}else{						// Kabur Kroco
							isMoved = escapePawnWhite(x,y);
						}
						
						if (!isMoved && moveState==1){
							isMoved = backupWhite(x,y);		// Kasih Backup
						}
					}else{
						if (Board[x][y]->isKing){
							isMoved = movePawnKingWhite(moveState,x,y);
						}else{
							isMoved = movePawnWhite(moveState,x,y);
						}
					}					
				}
				
				if (!isMoved){
					if (!isEmpty(LoDanger)){
						i++;
						if (i==sizeLOP(LoDanger)) { 
							i=0;
							x=(size/2)-1;
							y=-1;
						}
					}
					
					x--;
					if (x==-1){ x=(size/2)-1; y--; }
					if (y == -1){
						if (moveState==0){moveState=1;}
						if (moveState==1){
							moveState=2;
							clearList(&LoDanger);
						}
						x=(size/2)-1;
						y=size-1;
					}
				}
			}
		} else {
			i = 1;
			while (!isMoved){
				x = getX(LoKing,sizeLOP(LoKing)-i);
				y = getY(LoKing,sizeLOP(LoKing)-i);
				
				isMoved = movePawnKingWhite(moveState,x,y);
				
				if (i==sizeLOP(LoKing)){
					i=1;
					clearList(&LoKing);
					goto terpaksa;
				} else {
					i++;
				}
			}
		}
	}
}


void CheckerBoard::moveBlack3()
{
	bool isMoved = false;
	int x = 0, y = 0, i = 0;
	int moveState = 0; 			//0=belom jalan, 1=UdahGakAman, 2 =  Matilah
	ListOfPoint LoKing = NULL;
	searchKing(&LoKing);
	printList(LoKing);
	if (!isEmpty(LoEat)){
		checkJump(jumpAIx,jumpAIy);
		move(getX(LoJump,0),getY(LoJump,0),getX(LoJump,1),getY(LoJump,1));
	}else{
		if (isEmpty(LoKing) || !isEmpty(LoDanger)){
			while (!isMoved){
				terpaksa:
				if (isExistBlackPawn(x,y)){
					if(!isEmpty(LoDanger)){
						x = getX(LoDanger,i);
						y = getY(LoDanger,i);	
						isMoved = backupBlack(x,y);		// Kasih Backup
						if (!isMoved && moveState==1){
							if (Board[x][y]->isKing){			// Kabur King
								isMoved = escapePawnKingBlack(x,y);
							}else{						// Kabur Kroco
								isMoved = escapePawnBlack(x,y);
							}
						}
					}else { 
						if(moveState==0){
							isMoved = backupBlack(x,y);
						}
						else{
							isMoved = movePawnBlack(moveState,x,y);
						}
					}				
				}
				
				if (!isMoved){
					if (!isEmpty(LoDanger)){
						if (moveState==0){
							i++;
							if (i==sizeLOP(LoDanger) && moveState==0) { 
								i=0;
								x=0;
								y=size;
							}
						}
					}
					
					x++;
					if (x==size/2){ x=0; y++; }
					if (y == size){
						if (moveState==0){moveState=1;}
						if (moveState==1){
							moveState=2;
							clearList(&LoDanger);
						}
						x=0;
						y=0;
					}
				}
			}
		} else {
			i = 1;
			while (!isMoved){
				x = getX(LoKing,sizeLOP(LoKing)-i);
				y = getY(LoKing,sizeLOP(LoKing)-i);
				
				isMoved = movePawnKingBlack(moveState,x,y); 
				
				if (i==sizeLOP(LoKing)){
					i=1;
					clearList(&LoKing);
					goto terpaksa;
				}
				else{
					i++;
				}
			}
		}
	}
}

void CheckerBoard::moveWhite3()
{
	bool isMoved = false;
	int x = (size/2)-1, y = size-1, i = 0;
	int moveState = 0; 			//0=belom jalan, 1=UdahGakAman, 2 =  Matilah
	ListOfPoint LoKing = NULL;
	searchKing(&LoKing);
	printList(LoKing);
	if (!isEmpty(LoEat)){
		checkJump(jumpAIx, jumpAIy);
		move(getX(LoJump,0),getY(LoJump,0),getX(LoJump,1),getY(LoJump,1));
	}else{
		if (isEmpty(LoKing) || !isEmpty(LoDanger)){
			while (!isMoved){
				terpaksa:
				if (isExistWhitePawn(x,y)){
					if(!isEmpty(LoDanger)){ 
						x = getX(LoDanger,i);
						y = getY(LoDanger,i);
						
						if (Board[x][y]->isKing){			// Kabur King
							isMoved = escapePawnKingWhite(x,y);
						}else{						// Kabur Kroco
							isMoved = escapePawnWhite(x,y);
						}
						
						if (!isMoved && moveState==1){
							isMoved = backupWhite(x,y);		// Kasih Backup
						}
					}else { 
						if(moveState==0){
							isMoved = backupWhite(x,y);
						}
						else{
							isMoved = movePawnWhite(moveState,x,y);
						}
					}				
				}
				
				if (!isMoved){
					if (!isEmpty(LoDanger)){
						i++;
						if (i==sizeLOP(LoDanger)) { 
							i=0;
							x=(size/2)-1;
							y=-1;
						}
					}
					
					x--;
					if (x==-1){ x=(size/2)-1; y--; }
					if (y == -1){
						if (moveState==0){moveState=1;}
						if (moveState==1){
							moveState=2;
							clearList(&LoDanger);
						}
						x=(size/2)-1;
						y=size-1;
					}
				}
			}
		} else {
			i = 1;
			while (!isMoved){
				x = getX(LoKing,sizeLOP(LoKing)-i);
				y = getY(LoKing,sizeLOP(LoKing)-i);
				
				isMoved = movePawnKingWhite(moveState,x,y);
				
				if (i==sizeLOP(LoKing)){
					i=1;
					clearList(&LoKing);
					goto terpaksa;
				} else {
					i++;
				}
			}
		}
	}
}

Point CheckerBoard::searchNearestEnemy(Point pawnPos, bool side)
{
	int i, j;
	Point temp, tempS;
	temp.x = 20;
	temp.y = 20;
	for (i=0;i<=(size/2)-1;i++){
		for (j=0;j<=size-1;j++){
			if (j%2==0){
				if (isExistPawn(i,j) && j!=0 && j != size-1  && i!=0){
					if (Board[i][j]->side!=side){
						tempS.x = i;
						tempS.y = j;
						temp = minimumDistance(temp,tempS,pawnPos);
					}
				}
			} else{
				if (isExistPawn(i,j) && j!=0 && j != size-1  && i!=(size/2)-1){
					if (Board[i][j]->side!=side){
						tempS.x = i;
						tempS.y = j;
						temp = minimumDistance(temp,tempS,pawnPos);
					}
				}
			}
		}
	}
	return temp;
}

Point CheckerBoard::minimumDistance(Point P1, Point P2, Point pawnPos)
{
	if ((abs(P1.x-pawnPos.x)*2)+abs(P1.y-pawnPos.y) < (abs(P2.x-pawnPos.x)*2)+abs(P2.y-pawnPos.y)){
		return P1;
	}else {
		return P2;
	}
}

void CheckerBoard::searchKing(ListOfPoint* LoKing)
{
	int i,j;
	for (i=0;i<=(size/2)-1;i++){
		for (j=0;j<=size-1;j++){
			if ((turn==WHITE && isExistWhiteKingPawn(i,j)) || (turn==BLACK && isExistBlackKingPawn(i,j))){
				add(LoKing,i,j);
			}
		}
	}
}

bool CheckerBoard::isExistNormalPawn(){
	int i,j;
	for (i=0;i<=(size/2)-1;i++){
		for (j=0;j<=size-1;j++){
			if ((turn==WHITE && isExistWhitePawn(i,j)) || (turn==BLACK && isExistBlackPawn(i,j))){
				if (!Board[i][j]->isKing) {return true;}
			}
		}
	}
	return false;
}

bool CheckerBoard::isMoveSafetyBlack(int x, int y, int x0, int y0)
{
	if (y>size-1 || y<0 || x>(size/2)-1 || x<0){return false;}	
	if (isExistPawn(x,y)){return false;}
	if (y%2==1){
		if (y==size-1 || y==0 || x==(size/2)-1){return true;}
		else {
			if(!isExistWhitePawn(x,y-1)&&!isExistWhitePawn(x+1,y-1)&&!isExistWhiteKingPawn(x+1,y+1)&&!isExistWhiteKingPawn(x,y+1)){
				return true;
			}else if (isNoHopeBlack(x,y)){
				return false;
			}else if (isExistWhitePawn(x,y-1)){ 	  // musuh kiri atas
				return(isExistPawn(x+1,y+1) && (x0 != x+1 || y0 != y+1)); 		  // Cek kanan bawah
			}else if (isExistWhitePawn(x+1,y-1)){ 	  // musuh kanan atas
				return(isExistPawn(x,y+1) && (x0 != x || y0 != y+1 )); 		  // Cek kiri bawah
			}else if (isExistWhiteKingPawn(x+1,y+1)){ // king musuh kanan bawah
				return(isExistPawn(x,y-1) && (x0 != x || y0 != y-1)); 		  // Cek kiri atas
			}else if (isExistWhiteKingPawn(x,y+1)){   // king musuh kiri bawah
				return(isExistPawn(x+1,y-1) && (x0 != x+1 || y0 != y-1)); 		  // Cek kanan atas
			}else {
				return false;
			}
		}
	}else{
		if (y==size-1 || y==0 || x==0){return true;}
		else {
			if (!isExistWhitePawn(x-1,y-1)&&!isExistWhitePawn(x,y-1)&&!isExistWhiteKingPawn(x-1,y+1)&&!isExistWhiteKingPawn(x,y+1)){
				return true;
			}else if (isNoHopeBlack(x,y)){
				return false;
			}else if (isExistWhitePawn(x-1,y-1)){ 	  // musuh kiri atas
				return(isExistPawn(x,y+1)  && (x0 != x || y0 != y+1)); 	  	  // Cek kanan bawah
			}else if(isExistWhitePawn(x,y-1)){ 		  // musuh kanan atas
				return(isExistPawn(x-1,y+1)  && (x0 != x-1 || y0 != y+1)); 		  // Cek kiri bawah
			}else if (isExistWhiteKingPawn(x-1,y+1)){ // king musuh kiri bawah
				return(isExistPawn(x,y-1)  && (x0 != x || y0 != y-1)); 		  // Cek kanan atas
			}else if(isExistWhiteKingPawn(x,y+1)){ 	  // king musuh kanan bawah
				return(isExistPawn(x-1,y-1)  && (x0 != x || y0 != y-1)); 		  // Cek kiri atas
			}else {
				return false;
			}
		}
	}
}

bool CheckerBoard::isMoveSafetyWhite(int x, int y, int x0, int y0)
{
	if (y>size-1 || y<0 || x>(size/2)-1 || x<0){return false;}
	if (isExistPawn(x,y)){return false;}
	if (y%2==1){
		if (y==size-1 || y==0 || x==(size/2)-1){return true;}
		else {
			if(!isExistBlackPawn(x,y+1)&&!isExistBlackPawn(x+1,y+1)&&!isExistBlackKingPawn(x+1,y-1)&&!isExistBlackKingPawn(x,y-1)){
				return true;
			}else if (isNoHopeWhite(x,y)){
				return false;
			}else if (isExistBlackPawn(x,y+1)){ 	  // musuh kiri atas
				return(isExistPawn(x+1,y-1)  && (x0 != x+1 || y0 != y-1)); 		  // Cek kanan bawah
			}else if (isExistBlackPawn(x+1,y+1)){ 	  // musuh kanan atas
				return(isExistPawn(x,y-1) && (x0 != x || y0 != y-1)); 		  // Cek kiri bawah
			}else if (isExistBlackKingPawn(x+1,y-1)){ // king musuh kanan bawah
				return(isExistPawn(x,y+1) && (x0 != x || y0 != y+1)); 		  // Cek kiri atas
			}else if (isExistBlackKingPawn(x,y-1) ){   // king musuh kiri bawah
				return(isExistPawn(x+1,y+1) && (x0 != x+1 || y0 != y+1)); 		  // Cek kanan atas
			}else {
				return false;
			}
		}
	}else{
		if (y==size-1 || y==0 || x==0){return true;}
		else {
			if (!isExistBlackPawn(x-1,y+1)&&!isExistBlackPawn(x,y+1)&&!isExistBlackKingPawn(x-1,y-1)&&!isExistBlackKingPawn(x,y-1)){
				return true;
			}else if (isNoHopeWhite(x,y)){
				return false;
			}else if (isExistBlackPawn(x-1,y+1)){ 	  // musuh kiri atas
				return(isExistPawn(x,y-1) && (x0 != x || y0 != y-1)); 	  	  // Cek kanan bawah
			}else if(isExistBlackPawn(x,y+1)){ 		  // musuh kanan atas
				return(isExistPawn(x-1,y-1) && (x0 != x-1 || y0 != y-1)); 		  // Cek kiri bawah
			}else if (isExistBlackKingPawn(x-1,y-1)){ // king musuh kiri bawah
				return(isExistPawn(x,y+1) && (x0 != x || y0 != y+1)); 		  // Cek kanan atas
			}else if(isExistBlackKingPawn(x,y-1)){ 	  // king musuh kanan bawah
				return(isExistPawn(x-1,y+1) && (x0 != x-1 || y0 != y+1)); 		  // Cek kiri atas
			}else {
				return false;
			}
		}
	}
}

bool CheckerBoard::isNoHopeBlack(int x, int y)
{
	if (y%2==0){
		if (x!=0 && y!=0){
			return(isExistWhitePawn(x-1,y-1)&&isExistWhitePawn(x,y-1));
		}
	}else{
		if (x!=(size/2)-1 && y!=0){
			return(isExistWhitePawn(x,y-1)&&isExistWhitePawn(x+1,y-1));
		}
	}
}

bool CheckerBoard::isNoHopeWhite(int x, int y)
{
	if (y%2==0){
		if (x!=0 && y!=0){
			return(isExistBlackPawn(x-1,y+1)&&isExistBlackPawn(x,y+1));
		}
	}else{
		if (x!=(size/2)-1 && y!=0){
			return(isExistBlackPawn(x,y-1)&&isExistBlackPawn(x+1,y-1));
		}
	}
	
	return false;
}

bool CheckerBoard::movePawnWhite(int state, int x, int y)
{
	int X1,X2;
	if (y%2==0){ X1 = x-1; X2 = x;}
	else { X1 = x; X2 = x+1;}	
	
	if (state < 2){
		if (x>0 || X1==x){
			if (isMoveSafetyWhite(X1,y+1,x,y) && state==0){
				move(x,y,X1,y+1);
				return true;
			}
		}
		if (x<(size/2)-1 || X2==x){
			if (x<(size/2)-1){
				if (isMoveSafetyWhite(X2,y+1,x,y) && state==0){
					move(x,y,X2,y+1);
					return true;
				}
			}
		}
		return false;
	}else if(state>=2){
		if (x>0 || X1==x){
			if (isMoveAllowed(x,y,X1,y+1)){
				move(x,y,X1,y+1);
				return true;
			}
		}
		if (x<(size/2)-1 || X2==x){
			if (isMoveAllowed(x,y,X2,y+1)){
				move(x,y,X2,y+1);
				return true;
			}
		}
		
		return false;
	}
}

bool CheckerBoard::movePawnBlack(int state, int x, int y)
{
	int X1,X2;
	if (y%2==0){ X1 = x-1; X2 = x;}
	else { X1 = x; X2 = x+1;}	
	
	if (state < 2){
		if (x>0 || X1==x){
			if (isMoveSafetyBlack(X1,y-1,x,y) && state==0){
				move(x,y,X1,y-1);
				return true;
			}
		}
		if (x<(size/2)-1 || X2==x){
			if (isMoveSafetyBlack(X2,y-1,x,y) && state==0){
					move(x,y,X2,y-1);
					return true;
			}
		}
		return false;
	}else if(state>=2){
		if (x>0 || X1==x){
			if (isMoveAllowed(x,y,X1,y-1)){
				move(x,y,X1,y-1);
				return true;
			}
		}
		if (x<(size/2)-1 || X2==x){
			if (isMoveAllowed(x,y,X2,y-1)){
				move(x,y,X2,y-1);
				return true;
			}
		}
		
		return false;
	}
}

bool CheckerBoard::movePawnKingBlack(int state, int x, int y)
{
	int X1,X2,i=0;
	int* prio = (int*) malloc (4 * sizeof (int));
	Point temp;
	bool isMoved = false;
	if (y%2==0){ X1=x-1; X2=x; }
	else { X1=x; X2=x+1; }
	temp.x = x;
	temp.y = y;
	temp = searchNearestEnemy(temp,BLACK);
	if (temp.x==20 && temp.y==20){
		return false;
	}else{
		if (temp.x <= x && temp.y<=y){
			prio[0] = LT;
			prio[1] = LB;
			prio[2] = RT;
			prio[3] = RB;
		}else if (temp.x <= x && temp.y>y){
			prio[0] = LB;
			prio[1] = LT;
			prio[2] = RB;
			prio[3] = RT;
		}else if (temp.x > x && temp.y<=y){
			prio[0] = RT;
			prio[1] = RB;
			prio[2] = LT;
			prio[3] = LB;
		}else {
			prio[0] = RB;
			prio[1] = RT;
			prio[2] = LB;
			prio[3] = LT;
		}
		
		while (!isMoved && i<(size/2)){
			if (prio[i]==LT){
				if(isMoveSafetyBlack(X1,y-1,x,y)){
					move(x,y,X1,y-1);
					isMoved=true;
				}
			}else if (prio[i]==LB){
				if(isMoveSafetyBlack(X1,y+1,x,y)){
					move(x,y,X1,y+1);
					isMoved=true;
				}
			}else if (prio[i]==RT){
				if(isMoveSafetyBlack(X2,y-1,x,y)){
					move(x,y,X2,y-1);
					isMoved=true;
				}			
			}else if (prio[i]==RB){
				if(isMoveSafetyBlack(X2,y+1,x,y)){
					move(x,y,X2,y+1);
					isMoved=true;
				}			
			} 
			i++;
		}
		return isMoved;
	}
}

bool CheckerBoard::movePawnKingWhite(int state, int x, int y)
{
	int X1,X2,i=0;
	int* prio = (int*) malloc (4 * sizeof (int));
	Point temp;
	bool isMoved = false;
	if (y%2==0){ X1=x-1; X2=x; }
	else { X1=x; X2=x+1; }
	temp.x = x;
	temp.y = y;
	temp = searchNearestEnemy(temp,WHITE);
	if (temp.x==20 && temp.y==20){
		return false;
	}else{
		if (temp.x <= x && temp.y<=y){
			prio[0] = LT;
			prio[1] = RT;
			prio[2] = LB;
			prio[3] = RB;
		}else if (temp.x <= x && temp.y>y){
			prio[0] = LB;
			prio[1] = RB;
			prio[2] = LT;
			prio[3] = RT;
		}else if (temp.x > x && temp.y<=y){
			prio[0] = RT;
			prio[1] = LT;
			prio[2] = RB;
			prio[3] = LB;
		}else {
			prio[0] = RB;
			prio[1] = LB;
			prio[2] = RT;
			prio[3] = LT;
		}
		
		while (!isMoved && i<(size/2)){
			if (prio[i]==LT){
				if(isMoveSafetyWhite(X1,y-1,x,y)){
					move(x,y,X1,y-1);
					isMoved=true;
				}
			}else if (prio[i]==LB){
				if(isMoveSafetyWhite(X1,y+1,x,y)){
					move(x,y,X1,y+1);
					isMoved=true;
				}
			}else if (prio[i]==RT){
				if(isMoveSafetyWhite(X2,y-1,x,y)){
					move(x,y,X2,y-1);
					isMoved=true;
				}			
			}else if (prio[i]==RB){
				if(isMoveSafetyWhite(X2,y+1,x,y)){
					move(x,y,X2,y+1);
					isMoved=true;
				}			
			} 
			i++;
		}
		
		return isMoved;
	}
}

bool CheckerBoard::movePawnKingImmediate(int x, int y){
	int X1,X2,temp;
	if (y%2==0){ X1=x-1; X2=x; }
	else { X1=x; X2=x+1; }
	
	if (Board[x][y]->side==WHITE){
		if (isMoveAllowed(x,y,X1,y-1)){
			move(x,y,X1,y-1);
			return true;
		}
		if (isMoveAllowed(x,y,X2,y-1)){
			move(x,y,X2,y-1);
			return true;
		}
		if (isMoveAllowed(x,y,X1,y+1)){
			move(x,y,X1,y+1);
			return true;
		}
		if (isMoveAllowed(x,y,X2,y+1)){
			move(x,y,X2,y+1);
			return true;
		}
	} else {
		if (isMoveAllowed(x,y,X2,y+1)){
			move(x,y,X2,y+1);
			return true;
		}
		if (isMoveAllowed(x,y,X1,y+1)){
			move(x,y,X1,y+1);
			return true;
		}
		if (isMoveAllowed(x,y,X2,y-1)){
			move(x,y,X2,y-1);
			return true;
		}
		if (isMoveAllowed(x,y,X1,y-1)){
			move(x,y,X1,y-1);
			return true;
		}
	}
	
	return false;
}

bool CheckerBoard::backupBlack(int x, int y)
{
	int X1,X2;
	if (y%2==0){ X1=x-1; X2=x; }
	else { X1=x; X2=x+1; }
	
	if (isMoveSafetyBlack(X1,y+1,x-1,y+2)){
		if (isExistBlackPawn(x-1,y+2)){
			move(x-1,y+2,X1,y+1);
			return true;
		}
	}
	if (isMoveSafetyBlack(X1,y+2,x,y+2)){
		if (isExistBlackPawn(x,y+2)){
			move(x,y+2,X1,y+1);
			return true;
		}
	}
		
	if (isMoveSafetyBlack(X2,y+1,x+1,y+2)){
		if (isExistBlackPawn(x+1,y+2)){
			move(x-1,y+2,X2,y+1);
			return true;
		}
	}
	if (isMoveSafetyBlack(X2,y+1,x,y+2)){
		if (isExistBlackPawn(x,y+2)){
			move(x,y+2,X2,y+1);
			return true;
		}
	}

	return false;
}

bool CheckerBoard::backupWhite(int x, int y)
{
	int X1,X2;
	if (y%2==0){ X1=x-1; X2=x; }
	else { X1=x; X2=x+1; }
	
	if (isMoveSafetyWhite(X1,y-1,x-1,y-2)){
		if (isExistWhitePawn(x-1,y-2)){
			move(x-1,y-2,X1,y-1);
			return true;
		}
	}
	if (isMoveSafetyWhite(X1,y-1,x,y-2)){			
		if (isExistWhitePawn(x,y-2)){
			move(x,y-2,X1,y-1);
			return true;
		}
	}		
		
	if (isMoveSafetyWhite(X2,y-1,x+1,y-2)){
		if (isExistWhitePawn(x+1,y-2)){
			move(x-1,y-2,X2,y-1);
			return true;
		}
	}
	if (isMoveSafetyWhite(X2,y-1,x,y-2)){
		if (isExistWhitePawn(x,y-2)){
			move(x,y-2,X2,y-1);
			return true;
		}
	}
	
	return false;
}

bool CheckerBoard::escapePawnBlack(int x, int y)
{
	int X1,X2;
	if (y%2==0){ X1=x-1; X2=x; }
	else { X1=x; X2=x+1; }
	
	if (isNoHopeBlack(x,y)){
		return false;
	} 
	
	if (isExistWhiteKingPawn(X2,y+1)){	
		if (isMoveSafetyBlack(X1,y-1,x,y)){
			move (x,y,X1,y-1);
			return true;
		}
		if (isMoveSafetyBlack(X2,y-1,x,y)){
			move (x,y,X2,y-1);
			return true;
		}
	}
	
	if (isExistWhiteKingPawn(X1,y+1)){	
		if (isMoveSafetyBlack(X2,y-1,x,y)){
			move (x,y,X2,y-1);
			return true;
		}
		if (isMoveSafetyBlack(X1,y-1,x,y)){
			move (x,y,X1,y-1);
			return true;
		}
	}	
	
	if (isExistWhitePawn(X1,y-1)){	
		if (isMoveSafetyBlack(X2,y-1,x,y)){
			move (x,y,X2,y-1);
			return true;
		}
	}
	
	if (isExistWhitePawn(X2,y-1)){	
		if (isMoveSafetyBlack(X1,y-1,x,y)){
			move (x,y,X1,y-1);
			return true;
		}
	}
	
	return false;
}

bool CheckerBoard::escapePawnWhite(int x, int y)
{
	int X1,X2;
	if (y%2==0){ X1=x-1; X2=x; }
	else { X1=x; X2=x+1; }
	
	if (isNoHopeWhite(x,y)){
		return false;
	} 
	
	if (isExistBlackKingPawn(X2,y-1)){	
		if (isMoveSafetyWhite(X1,y+1,x,y)){
			move (x,y,X1,y+1);
			return true;
		}
		if (isMoveSafetyWhite(X2,y+1,x,y)){
			move (x,y,X2,y+1);
			return true;
		}
	}
		
	if (isExistBlackKingPawn(X1,y-1)){	
		if (isMoveSafetyWhite(X2,y+1,x,y)){
			move (x,y,X2,y+1);
			return true;
		}
		if (isMoveSafetyWhite(X1,y+1,x,y)){
			move (x,y,X1,y+1);
			return true;
		}
	}
	
	if (isExistBlackPawn(X2,y+1)){	
		if (isMoveSafetyWhite(X1,y+1,x,y)){
			move (x,y,X1,y+1);
			return true;
		}
	}
	
	if (isExistBlackPawn(X1,y+1)){	
		if (isMoveSafetyWhite(X2,y+1,x,y)){
			move (x,y,X2,y+1);
			return true;
		}
	}		
	
	return false;
}

bool CheckerBoard::escapePawnKingBlack(int x, int y)
{
	int X1,X2;
	if (y%2==0){ X1=x-1; X2=x; }
	else { X1=x; X2=x+1; }
	
	if (isExistWhitePawn(X1,y-1)){	
		if (isMoveSafetyBlack(X1,y+1,x,y)){
			move (x,y,X1,y+1);
			return true;
		}
		
		if (isMoveSafetyBlack(X2,y+1,x,y)){
			move (x,y,X2,y+1);
			return true;
		}
	}
	
	if (isExistWhitePawn(X2,y-1)){	
		if (isMoveSafetyBlack(X2,y+1,x,y)){
			move (x,y,X2,y+1);
			return true;
		}
			
		if (isMoveSafetyBlack(X1,y+1,x,y)){
			move (x,y,X1,y+1);
			return true;
		}
	}
	return(escapePawnBlack(x,y));
}

bool CheckerBoard::escapePawnKingWhite(int x, int y)
{
	int X1,X2;
	if (y%2==0){ X1=x-1; X2=x; }
	else { X1=x; X2=x+1; }
	
	if (isExistBlackPawn(X1,y+1)){	
		if (isMoveSafetyWhite(X1,y+1,x,y)){
			move (x,y,X1,y+1);
			return true;
		}
		
		if (isMoveSafetyWhite(X2,y+1,x,y)){
			move (x,y,X2,y+1);
			return true;
		}
	}
	
	if (isExistBlackPawn(X2,y+1)){	
		if (isMoveSafetyWhite(X2,y+1,x,y)){
			move (x,y,X2,y+1);
			return true;
		}
			
		if (isMoveSafetyWhite(X1,y+1,x,y)){
			move (x,y,X1,y+1);
			return true;
		}
	}
	return(escapePawnWhite(x,y));
}
